/**
 * Created by 荆安喜 on 2017/8/28.
 */
$(function () {
    var ad = appData(),
        pageNumNow = 1,
        paramsT,
        dataPages
    $(function () {
        //初始化渲染
        var parms = {
            deviceState: "-1",
            deviceName: "设备名称",
            deviceText: "",
            deviceType: "-1"
        }
        paramsT = parms
        //正常启动占比例
        ad.selectList('KMGA_DEVICE_LIST', {}, darwDeviceList);
        //设备分页渲染
        showTable(true, 1, 'KMGA_DEVICE_PAGE', 'devicePageList')

        //设备检索
        $('#deviceSearch').on('click', deviceSearch)

        //添加设备
        $('#save').on('click', deviceSave)

        $('#default').on('click', function () {
            $(".dialog").hide()
        })

        //渲染设备类型
        ad.selectList('KMGA_GET_DEVICETYPE', {}, deviceType)

        //渲染设备厂商
        ad.selectList('KMGA_GET_DEVICEVENDER', {}, deviceVender)
    })


    //渲染设备列表
    function darwDeviceList(data) {
        $('.panel-title label').remove()
        for (var key in data) {
            $('.panel-title').append("<label>" + data[key].name + "  " + "<em>" + data[key].running + "/" + data[key].total + "</em></label>")
        }
    }

    //检索拓扑图
    function deviceSearch(d, i) {
        var parms = {
            deviceState: $('#deviceState').val(),
            deviceName: $('#deviceName').val(),
            deviceText: $('#deviceText').val(),
            deviceType: $('#deviceType').val()
        }
        paramsT = parms
        ad.selectList('KMGA_DEVICE_LIST', parms, darwDeviceList)

        //分页
        showTable(true, 1, 'KMGA_DEVICE_PAGE', 'devicePageList')
    }

    //分页信息
    function showTable(dontRenderPager, page, url, domId) {
        paramsT.page = page
        paramsT.pageSize = 15
        ad.selectList(url, paramsT, function (data) {
            $("#" + domId).empty();
            if (data.content.length === 0) {
                $("#" + domId).append("<tr><td colspan='12'  class='colspanCss'><h1>暂无数据</h1></td></tr>");
                $("#pageList").empty();
                return;
            }

            if (data.content <= appApi("PAGE_SIZE")) {
                $("#pageList").empty();
                return;
            }
            $.get('scripts/app/safe/homePage/devicePageList.tmpl', function (tmpl) {
                $("#" + domId).empty();
                dataPages = data
                $.tmpl(tmpl, data).appendTo("#" + domId);
                //配置事件
                $('.icon-setting').on('click', deploy)
                //删除事件
                $('.icon-delete').on('click', delDevice)
                //table 排序插件
                $('table').tablesort().data('tablesort');
                //批量操作控件使用
                var bat = new batch('checkAll')
                bat.delete('delAll', batchDel)
                //bat.restart('startAll', batchStart)
               // bat.close('closeAll', batchClose)
                if (dontRenderPager) {
                    renderPager(data.totalRecord);
                }
            })

        });
    }

    function renderPager(totalRecord, nowPage) {
        $("#pageList").pagination(totalRecord, appApi("PAGE_SIZE"), {
            callback: function (page_id, jq) {
                pageNumNow = page_id + 1;
                $('#checkAll').iCheck('uncheck')
                showTable(false, pageNumNow, 'KMGA_DEVICE_PAGE', 'devicePageList');
            }
        }, nowPage);
    }

    var parms = {}
    //添加设备
    function deviceSave(d) {

        var name = $(this.parentElement.parentElement.parentElement.parentElement).find('span').html()
        parms = {
            "device_name": $('#addDeviceName').val(),
            "device_ip": $('#addDeviceIp').val(),
            "device_mac": $('#addDeviceMac').val(),
            "device_type": $('#addDeviceType').val() == -1 ? '' : $('#addDeviceType').val(),
            "device_location": $('#addDeviceLocat').val(),
            "device_adminName": $('#deviceaDminName').val(),
            "device_adminPhone": $('#devicePhone').val(),
            "device_userName": $('#userName').val(),
            "device_userPassword": $('#userPassword').val(),
            "device_vers":$('#addDeviceVersion').val(),
            "id": parms.id,
            "device_manuf": $('#addDeviceVender').val(),
        }
        if (name == '添加设备') {
            if ($('.R1').length != 0) {
                return _as.jAlert("<p>输入有错误</p>");
            }
            //添加设备
            ad.insert('KMGA_DEVICE_ADD', parms, function (data) {
                //清空之前数据
                $('#addDeviceName').val('')
                $('#addDeviceIp').val('')
                $('#addDeviceMac').val('')
                $('#addDeviceType').val('')
                $('#addDeviceLocat').val('')
                $('#deviceaDminName').val('')
                $('#devicePhone').val('')
                $('#userName').val('')
                $('#userPassword').val('')
                $('#addDeviceVender').val('')
                $('#addDeviceVersion').val('')
                $(".dialog").hide()

                showTable(true, pageNumNow, 'KMGA_DEVICE_PAGE', 'devicePageList')
            })
        }

        if (name == '配置设备') {
            if ($('.R1').length != 0) {
                return _as.jAlert("<p>输入有错误</p>");
            }
            //更新设备
            ad.update('KMGA_DEVICE_UPDATA', parms, function (data) {
                //清空之前数据
                $('#addDeviceName').val('')
                $('#addDeviceIp').val('')
                $('#addDeviceMac').val('')
                $('#addDeviceType').val('')
                $('#addDeviceLocat').val('')
                $('#deviceaDminName').val('')
                $('#devicePhone').val('')
                $('#userName').val('')
                $('#userPassword').val('')
                $('#addDeviceVender').val('')
                $('#addDeviceVersion').val('')
                $(".dialog").hide()
                showTable(true, pageNumNow, 'KMGA_DEVICE_PAGE', 'devicePageList')

            })
        }

    }


    //配置页面
    function deploy() {
        $('.R1').remove()
        $('#userName').attr('disabled', true)
        $('#userPassword').attr('disabled', true)

        $('.dialog-head span').html('设备信息编辑')
        var id = $(this).attr('data-id')
        parms.id = id
        dataPages.content.forEach(function (d, i) {
            if (d.device_id == id) {
                $('#addDeviceName').val(d.device_name)
                $('#addDeviceIp').val(d.device_ip)
                $('#addDeviceMac').val(d.device_mac)
                deviceTypeS.every(function (h,i) {
                    if(h.value==d.device_type){
                        $('#addDeviceType').val(h.key)
                        return false
                    }
                    $('#addDeviceType').val('-1')
                    return true
                })
                deviceVenderS.every(function (h,i) {
                    if(h.value==d.device_manuf){
                        $('#addDeviceVender').val(h.key)
                        return false
                    }
                    $('#addDeviceVender').val('-1')
                    return true
                })


                $('#addDeviceLocat').val(d.device_location)
                $('#deviceaDminName').val(d.device_adminName)
                $('#devicePhone').val(d.device_adminPhone)
                $('#userName').val(d.device_userName)
                $('#userPassword').val(d.device_userPassword)
                $('#addDeviceVersion').val(d.device_vers)
            }
        })
        $(".dialog").show()
    }

    //删除事件
    function delDevice() {
        var id = $(this).attr('data-id')
        _as.jConfirm('确定是否要删除',function(){
            ad.insert('KMGA_DEVICE_DEL', {id: id}, function () {
                showTable(true, pageNumNow, 'KMGA_DEVICE_PAGE', 'devicePageList')
            })
        })
    }


    //批量删除事件
    function batchDel(parms) {
        ad.insert('KMGA_BATCH_DEL', parms, function () {
            showTable(false, pageNumNow, 'KMGA_DEVICE_PAGE', 'devicePageList');
            $('#checkAll').iCheck('uncheck')
            _as.jAlert("<p>删除成功</p>")
        })
    }

    //批量重启事件
    function batchStart(parms) {
        ad.insert('KMGA_BATCH_START', parms, function () {
            showTable(false, pageNumNow, 'KMGA_DEVICE_PAGE', 'devicePageList');
            $('#checkAll').iCheck('uncheck')
            _as.jAlert("<p>重启成功</p>")
        })
    }

    //批量关闭事件
    function batchClose(parms) {
        ad.insert('KMGA_BATCH_CLOSE', parms, function () {
            showTable(false, pageNumNow, 'KMGA_DEVICE_PAGE', 'devicePageList');
            $('#checkAll').iCheck('uncheck')
            _as.jAlert("<p>关闭成功</p>")
        })
    }
    var deviceTypeS,deviceVenderS;
    //渲染设备类型
    function deviceType(data) {
        deviceTypeS=data
        data.forEach(function (d, i) {
            $('#addDeviceType').append("<option value='" + d.key + "'>" + d.value + "</option>")
        })
    }


    //渲染设备厂商
    function deviceVender(data) {
        deviceVenderS=data
        data.forEach(function (d, i) {
            $('#addDeviceVender').append("<option value='" + d.key + "'>" + d.value + "</option>")
        })
    }

})